﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AhpilyServer;
using AhpilyServer.Concurrent;

namespace GameServer.Cache
{
    /// <summary>
    /// 角色数据缓存层
    /// </summary>
   public class PlayerCache
    {
        /// <summary>
        /// 角色id 对应的 角色数据模型
        /// </summary>
        private Dictionary<int, PlayerModle> idModelDict = new Dictionary<int, PlayerModle>();
        /// <summary>
        /// 账号id对应的账号数据模型
        /// </summary>
        private Dictionary<int, int> accIdUIdDict = new Dictionary<int, int>();
        /// <summary>
        /// 存储在线玩家 只有在线玩家才有这个对象
        /// </summary>
        private Dictionary<int, ClientPeer> idClientDict = new Dictionary<int, ClientPeer>();
        private Dictionary<ClientPeer, int> clientIdDict = new Dictionary<ClientPeer, int>();

        /// <summary>
        /// 作为角色的id
        /// </summary>
        ConcurrentInt id = new ConcurrentInt(-1);


        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="name">角色名</param>
        /// <param name="accountId">账号id</param>
        public void Create(string name, int accountId)
        {

        }
        /// <summary>
        /// 判断账号下是否有角色
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public bool IsExist(int accountId)
        {
            return accIdUIdDict.ContainsKey(accountId);
        }
        /// <summary>
        /// 根据账号id获取角色数据模型
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public PlayerModle GetModelByAccountId(int accountId)
        {
            int userId = accIdUIdDict[accountId];
            PlayerModle model = idModelDict[userId];
            return model;        
        }
        /// <summary>
        /// 根据连接对象获取角色数据模型
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public PlayerModle GetModelByClientPeer(ClientPeer client)
        {
            int id = clientIdDict[client];
            PlayerModle model = idModelDict[id];
            return model;
        }
        /// <summary>
        /// 根据账号id获取角色id
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public int GetId(int accountId)
        {
            return accIdUIdDict[accountId];   
        }
        /// <summary>
        ///根据角色id获取连接对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ClientPeer GetClientPeer(int id)
        {
            return idClientDict[id];
        }
        /// <summary>
        ///根据在线玩家的连接对象 获取角色id
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public int Getid(ClientPeer client)
        {
            if (!clientIdDict.ContainsKey(client))
            {
                throw new IndexOutOfRangeException("玩家不在线");
            }
            return clientIdDict[client];
        }

        /// <summary>
        /// 判断客户端是否在线 根据连接对象
        /// </summary>
        /// <param name="client">客户端连接对象</param>
        /// <returns></returns>
        public bool IsOnline(ClientPeer client)
        {
            return clientIdDict.ContainsKey(client);
        }
        /// <summary>
        /// 判断客户端是否在线
        /// </summary>
        public bool IsOnline(int id)
        {
            return idClientDict.ContainsKey(id);
        }
        /// <summary>
        /// 角色上线
        /// </summary>
        /// <param name="client"></param>
        /// <param name="id"></param>
        public void Online(ClientPeer client, int id)
        {
            idClientDict.Add(id, client);
            clientIdDict.Add(client, id);
        }
        /// <summary>
        /// 角色下线
        /// </summary>
        /// <param name="client"></param>
        public void Offline(ClientPeer client)
        {
            int id = clientIdDict[client];
            clientIdDict.Remove(client);
            idClientDict.Remove(id);
        }

    }
}
